﻿package com.edward.action;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.Client;
import com.edward.model.InvoiceItem;
import com.edward.model.InvoiceList;
import com.edward.service.ClientManager;
import com.edward.service.InvoiceItemManager;
import com.edward.service.InvoiceListManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class InvoiceListAction extends ActionSupport{

	private static final long serialVersionUID = 2394035739961699269L;
	
	private InvoiceItemManager invoiceItemManager;
	
	private InvoiceListManager invoiceListManager;
	
	private ClientManager clientManager;
	
	private InvoiceList invoiceList;
	
	private InvoiceItem invoiceItem;
	
	private boolean flag=false;
	
	private int limit;
	
	private int start;
	
	private String query;
	
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query;
	}

	public InvoiceList getInvoiceList() {
		return invoiceList;
	}

	public void setInvoiceList(InvoiceList invoiceList) {
		this.invoiceList = invoiceList;
	}

	public InvoiceItem getInvoiceItem() {
		return invoiceItem;
	}

	public void setInvoiceItem(InvoiceItem invoiceItem) {
		this.invoiceItem = invoiceItem;
	}

	public void setInvoiceItemManager(InvoiceItemManager invoiceItemManager) {
		this.invoiceItemManager = invoiceItemManager;
	}

	public void setInvoiceListManager(InvoiceListManager invoiceListManager) {
		this.invoiceListManager = invoiceListManager;
	}
	
	public void setClientManager(ClientManager clientManager) {
		this.clientManager = clientManager;
	}

	//添加invoiceList 的表头
	@Secured("ROLE_ORDER_ADD")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		invoiceList.setInvoiceNum(invoiceListManager.listNum());
		invoiceListManager.addInvoiceList(invoiceList);
		//添加相应客户信息
		if(clientManager.check(invoiceList.getCompany()) == null){
			Client c = new Client();
			c.setAddress(invoiceList.getAddress());
			c.setCompany(invoiceList.getCompany());
			c.setContact(invoiceList.getContact());
			c.setEmail(invoiceList.getEmail());
			c.setLinkman(invoiceList.getLinkman());
			clientManager.addClient(c);
		}                                                                                                                                                                                                                                        
		int total = invoiceListManager.listInvoiceList().size();
		response.getWriter().print("{success : true , msg : '添加成功!',invoiceList_id:"+ invoiceList.getId()+",total:" + total + "}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String update() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InvoiceList s = invoiceListManager.getInvoiceList(invoiceList.getId());
		invoiceList.setItems(s.getItems());
		
		Client c = clientManager.check(s.getCompany());
			c.setAddress(invoiceList.getAddress());
			c.setCompany(invoiceList.getCompany());
			c.setContact(invoiceList.getContact());
			c.setEmail(invoiceList.getEmail());
			c.setLinkman(invoiceList.getLinkman());
			clientManager.updateClient(c);
	 
		invoiceListManager.updateInvoiceList(invoiceList);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String delete() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InvoiceList s = invoiceListManager.getInvoiceList(invoiceList.getId());
		if(s.getItems().size() != 0){
			response.getWriter().print("{success : false , msg : '删除失败，存在销售货物列表，请先删除销售货物!'}");
		}else{
			invoiceListManager.deleteInvoiceList(s.getId());
			response.getWriter().print("{success : true , msg : '删除成功!'}");
		}
		return null;
	}

	@Secured("ROLE_ORDER")
	public String getList() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
	    JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
		if(query == null){
			//如果flag为true 通过查询窗口过滤数据
			if(flag){
				pager = invoiceListManager.pagerList(start, limit,invoiceList );
			}else{
				pager = invoiceListManager.pagerList(start, limit );
			}
		}else{
			pager = invoiceListManager.pagerList(start,limit,query);
		}
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	
	
//	//添加invoiceList 的 货物明细
	@Secured("ROLE_ORDER_ADD")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InvoiceList s = invoiceListManager.getInvoiceList(invoiceList.getId());
		invoiceItem.setAmount(invoiceItem.getUnitPrice() * invoiceItem.getQty());
		invoiceItemManager.addInvoiceItem(invoiceItem);
		s.setTotal(s.getTotal() + invoiceItem.getAmount()); 
		s.getItems().add(invoiceItem);
		invoiceListManager.updateInvoiceList(s);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String updateItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		  HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InvoiceItem i =  invoiceItemManager.getInvoiceItem(invoiceItem.getId());
		invoiceItem.setAmount(invoiceItem.getUnitPrice() * invoiceItem.getQty());
		invoiceItem.setCount(i.getCount());
		InvoiceList s = invoiceListManager.getInvoiceList(invoiceList.getId());
		s.setTotal(s.getTotal() + invoiceItem.getAmount() - i.getAmount()); 
		invoiceListManager.updateInvoiceList(s);
		invoiceItemManager.updateInvoiceItem(invoiceItem);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_ORDER_UPDATE")
	public String deleteItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		InvoiceItem i = invoiceItemManager.getInvoiceItem(invoiceItem.getId());
		InvoiceList s = invoiceListManager.getInvoiceList(invoiceList.getId());
		s.setTotal(s.getTotal() - i.getAmount() ); 
		invoiceListManager.updateInvoiceList(s);
		invoiceItemManager.deleteInvoiceItem(i.getId());
		response.getWriter().print("{success : true , msg : '删除成功!'}");
		return null;
	}

	//获取grid 显示 的 分页信息
	@Secured("ROLE_ORDER")
	public String getItems() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = invoiceItemManager.pagerInvoiceItem(start, limit,invoiceList.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public String getClients() throws Exception {
		
		
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		List list = clientManager.getClients(query);
		  String json="";
		  JSONArray jsonArray;
		  jsonArray = JSONArray.fromObject(list);
		  json = jsonArray.toString();
		json  = "{\"names\":"  + json + "}";
		response.getWriter().print(json);
		return null;
	}
	
	//获取公司名称下拉框自动提示信息
	public String getClient() throws Exception {
		
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Client  c = clientManager.getClient(query);
		JSONObject jsonObject = JSONObject.fromObject(c);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_ORDER")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	
	
	
	
	
	
	

}
